﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using community.LanFen;


namespace community
{
    public partial class login : Form
    {
        //用户名和用户权限要传到main窗体中
        public string userId = "";

        SqlConnection conn = new SqlConnection (SQL.conStr);
        SqlCommand com;

        public login()
        {
            InitializeComponent();
        }

        private void index_enter()
        {
          //根据用户名，取出相关信息。
            using (conn = new SqlConnection(SQL.conStr))
            {
                string txt = "select LASTNAME_VCHR, DEPT_NAME_VCHR, RIGHT_INT ,PSW_CHR  from T_BSE_EMPLOYEE where  EMPID_CHR =  @User_uid";
                com = conn.CreateCommand();

                com.CommandText = txt;
                com.Parameters.AddWithValue("@User_uid", userName.Text.Trim());
                //com.Parameters.AddWithValue("@pwd",dataBase.encode(textBox2.Text.Trim()));
                    int rights = 0 ;
                    string uidName ="", userDept="",UserPwd = "";
                try
                {

                    if (conn.State == ConnectionState.Closed)
                        conn.Open();
                    SqlDataReader rdr = com.ExecuteReader();

                    while (rdr.Read())
                    {
                        if (rdr["RIGHT_INT"] == DBNull.Value)
                        {
                            userId = userName.Text;
                            userName.Items.Add(userId );
                            MessageBox.Show("用户权限不能为空！");
                            return;
                        }
                        else
                        {
                            
                            uidName = rdr["LASTNAME_VCHR"].ToString();//用户姓名
                            userDept = rdr["DEPT_NAME_VCHR"].ToString();//科室名
                            rights = Convert.ToInt32(rdr["RIGHT_INT"]);//权限
                            UserPwd = rdr["PSW_CHR"].ToString();
                        }
                    }
                }catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

              //
                if (UserPwd.Trim() ==  dataBase.encode ( textBox2.Text.Trim()))
                {

                    logininformatiom.userId = userName.Text.Trim();
                    logininformatiom.name = uidName;
                    logininformatiom.deptName = userDept;
                    if (rights == 1)
                    {
                        logininformatiom.right = "管理员";
                        //logininformatiom.index_visible = true;
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else if (rights == 2)
                    {
                        logininformatiom.right = "普通用户"; //权限=2
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }

                }
                else
                {
                    userId = userName.Text;
                    userName.Items.Add(userId);
                    MessageBox.Show("密码错误！");
                    textBox2.Clear();//清空密码框
                    //return;
                }

                

            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            index_enter();
        }

        private void login_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                index_enter();
            }
        }



        //protected override bool ProcessDialogKey(Keys keyData)//按enter键时，光标跳到下一个控件输入
        //{
        //    if (keyData == Keys.Enter)//&& !(ActiveControl is Button))
        //    {//碰到Button时不跳转到下一个控件，而是执行Button的单击事件
        //        keyData = Keys.Tab;
        //    }
        //    return base.ProcessDialogKey(keyData);
        //} 
    }
}
